{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "88f9b709-3d10-4d7a-bcdf-4594f4435da8",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "class ReferenceFrame:\n",
    "    def __init__(self, velocity=0):\n",
    "        self.v = velocity  # 速度，以光速 c 为单位\n",
    "        self.gamma = 1 / sqrt(1 - velocity**2) if velocity != 1 else infinity  # 洛伦兹因子\n",
    "\n",
    "    def relative_velocity(self, other):\n",
    "        \"\"\"计算另一参考系相对于当前参考系的相对速度\"\"\"\n",
    "        return (other.v - self.v) / (1 - self.v * other.v)\n",
    "        # 其中一个速度是0的话，另一个自然就是原来的v或-v了\n",
    "        # 相互之间的相对速度，交换之后只有符号差别而没有数值差别\n",
    "        # 不过确实不是简单的减法了，而是有个分母\n",
    "        # 这个分母具体是什么东西……我自己还不太清楚\n",
    "\n",
    "    def lorentz_transform(self, t, x, other):\n",
    "        \"\"\"将事件 (t, x) 从当前参考系变换到另一参考系，返回 (t', x')\"\"\"\n",
    "        u = self.relative_velocity(other)\n",
    "        gamma = 1 / sqrt(1 - u**2) if u != 1 else infinity\n",
    "        t_prime = gamma * (t - u * x)\n",
    "        x_prime = gamma * (x - u * t)\n",
    "        return (t_prime, x_prime)\n",
    "\n",
    "    def time_dilation(self, proper_time, other):\n",
    "        \"\"\"计算原时 (当前系) 在另一参考系中的时间膨胀\"\"\"\n",
    "        u = abs(self.relative_velocity(other))  # 速度取绝对值\n",
    "        gamma = 1 / sqrt(1 - u**2) if u != 1 else infinity\n",
    "        return gamma * proper_time\n",
    "\n",
    "    def length_contraction(self, proper_length, other):\n",
    "        \"\"\"计算原长 (当前系) 在另一参考系中的长度收缩\"\"\"\n",
    "        u = abs(self.relative_velocity(other))  # 速度取绝对值\n",
    "        gamma = 1 / sqrt(1 - u**2) if u != 1 else infinity\n",
    "        return proper_length / gamma\n",
    "\n",
    "    def velocity_addition(self, w, other):\n",
    "        \"\"\"将当前参考系中的速度 w 转换为另一参考系中的速度\"\"\"\n",
    "        u = self.relative_velocity(other)\n",
    "        return (w - u) / (1 - w * u)\n",
    "        # 纵向速度叠加，书上19页\n",
    "        # 另外在20页还有横向的……横向这边的推导有点离谱（太复杂了）\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4b980b4e-8cf6-4e68-8d16-deafefdba763",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<class '__main__.ReferenceFrame'>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.5\n",
    "S0=ReferenceFrame(0)\n",
    "S1=ReferenceFrame(0.6)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83349189-255c-4600-9251-7590b94a7272",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 10.5",
   "language": "sage",
   "name": "sagemath"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
